<?php
/**
 * The Template for displaying product archives, including the main shop page which is a post type archive.
 *
 * Override this template by copying it to yourtheme/woocommerce/archive-product.php
 *
 * @author 		WooThemes
 * @package 	WooCommerce/Templates
 * @version     2.0.0
 */
if (!defined('ABSPATH'))
    exit; // Exit if accessed directly

get_header('shop');
?>
<script type="text/javascript">
    $('.header').addClass('product shop-online');
</script>
<div class="row">
    <div class="col-lg-4 col-sm-4 col-12 header-description align-right">
        <div class="title step">
            <span class="xgoogleplus" style="display:inline-block;width:66px;"><script type="text/javascript" src="https://apis.google.com/js/plusone.js" gapi_processed="true">{lang: 'vi'}</script><g:plusone size="medium" ></g:plusone></span>
            <h2 class="line_1 hidden-sm"><?php woocommerce_page_title(); ?></h2>
            <h2 class="line_1 visible-sm" style="text-align: center;">
            <?php woocommerce_page_title(); ?>
            </h2>
        </div>
    </div>
</div>
</div>
</div>

<?php   
$option = get_option('kkfashion_1_settings');
if(is_shop()){  // if on shop page, just show the categories 
 ?>
 <div class="container">
    <div class="row">
	<?php echo do_shortcode('[product_categories number="12" orderby="name" order="DESC"]'); ?>
	</div>
 <?php 
	
}else{   //if is not the shop page, show the products  ?>
		
<div class="main-content container shopx">
    <div class="row">
        <?php
		
        wp_reset_query();
        global $wp_query, $product;
       
        $args = array_merge($wp_query->query_vars, array('posts_per_page' => '6',  'orderby' => 'ID', 'order' => 'DESC'));
        query_posts($args);

        if (have_posts()) :
            $i = 0;
            while (have_posts()) : the_post();

                if ($i == 0) {
                    $post_id = get_the_ID();
                    $get_regular_price = get_post_meta($post_id, '_price', true);
                    $get_regular_price = $get_regular_price ? $get_regular_price : 0; //echo "a{$get_regular_price}b";  die; 
                    $get_sku = get_post_meta($post_id, '_sku', true);
                    // $tags = get_the_term_list($post_id, 'product_tag', '', ', ', '');
                        switch (single_term_title( "", false )) {
                            case 'Đầm công sở':
                                $type_tags = 'dcs_tags';
                                break;
                            case 'Đầm dạ hội':
                                $type_tags = 'ddh_tags';
                                break;
                            case 'Phụ kiện':
                                $type_tags = 'pk_tags';
                                break;
                            case 'Váy chống nắng':
                                $type_tags = 'vcn_tags';
                                break;
                            default:
                                $type_tags = 'footer_tags';
                                break;
                        }
                        $tags = '';
                        foreach(explode(",", $option[$type_tags]) as $tag ){
                            $tagurl = get_site_url().'/product-tag/'.str_replace(' ','-',trim(vn_str_filter($tag)));
                            $tags .= "<a href='$tagurl'>$tag</a>, "; 
                        }
                    ?>
                    <div id="xprodetail" class="prodetail">

                        <div class=" col-lg-3 col-sm-3 col-12   hidden-sm">
                            <h3 class="contact-name"><?php echo get_the_title() . ' ' . $get_sku ?></h3>
                            <div class="note"><?php the_content() ?></div>
							<?php if($tags != '' ) { ?>
                            <p class="xtags">Tags: <?php echo $tags; ?></p>
							<?php } ?>
                            <p id="prosize">
                                <span class="size active" itemid="s" >S</span>
                                <span class="size" itemid="m" >M</span>
                                <span class="size" itemid="l" >L</span>
                                <span class="size xl" itemid="xl" >XL</span>
                            </p>
                             <?php if ($get_regular_price) { ?>
                                <p class="price">
                                    <span class="pull-left price-right"><?php echo number_format($get_regular_price, 0, ',', '.'); ?> VNĐ</span>
                                    <span class="pull-right hidden-md"><?php echo $get_sku; ?></span>
                                </p>
                                <div>

                                    <div class="col-lg-5 col-sm-12 product-cart no-right">
                                        <a href="<?php echo site_url() ?>/cart">Giỏ hàng (<span id="numpro"><?php echo $woocommerce->cart->cart_contents_count; ?></span>)</a>
                                    </div>
                                    <div class="col-lg-7 no-right ycart">
                                        <?php do_action('woocommerce_single_product_summary') ?>
                                    </div>

                                </div> 
                            <?php } else { ?>
                                <div class="outofpro">HẾT HÀNG</div>
                            <?php } ?>
                            <div class="clearfix"></div>
							
                            <div class="social-review hidden-sm">
								<div class="col-12 product-social no-right">
									Gửi mail cho bạn bè:
									<a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo urlencode(get_permalink()) ?>"><img alt="FB Share" title="FB Share" src="<?php echo bloginfo('template_url'); ?>/img/fb_share.png"></a>
									  <a href="https://twitter.com/share?url=<?php echo urlencode(get_permalink()) ?>" target="_blank"><img alt="Twitter Share" title="Twitter Share" src="<?php echo bloginfo('template_url'); ?>/img/tw_share.png"></a>
									  <a href="http://pinterest.com/pin/create/button/?url=<?php echo urlencode(get_permalink()) ?>&media=<?php echo urlencode(get_mthumb_link($get_sku, '356_520')) ?>&description=<?php the_title() ?>" class="pin-it-button" count-layout="horizontal">
									  <img alt=" Pinterest Share" title=" Pinterest Share" src="<?php echo bloginfo('template_url'); ?>/img/pr_share.png">
										</a>
										<a href="javascript:void(0);" onclick="window.print()" ><img alt="In"   src="<?php echo bloginfo('template_url'); ?>/img/printer.png"></a>
									
									
								</div>
							</div>
                                <!-- AddThis Button END -->
                            <div class="descpro"><?php do_action('woocommerce_single_product_excerpt') ?></div>
                            
                            <div class="clearfix"></div>
                        </div>

                        <div class="col-lg-5 col-sm-5 col-12 m10 hidden-sm">
                            <div class="thumb-product m10">
                                <a href="<?php the_permalink() ?>">
                                    <?php //the_post_thumbnail(array(356, 520), array('class' => 'img-thumbnail')) ?>
                                    <img src="<?php echo get_mthumb_link($get_sku, '356_520'); ?>" class="img-thumbnail" />
                                </a>
                            </div>
                            <div class="col-lg-9 col-sm-9 no-left"><h4><?php the_title() ?></h4></div>
                            <div class="col-lg-3 col-sm-3 no-left">
                                <div class="btn btn-custom"><a href="<?php the_permalink() ?>">Chi tiết</a></div>
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-4 col-sm-4 col-12 m10 list-product">

                        <?php
                    }
                    echo!$i || !($i % 2) ? '<div class="row">' : '';
                    woocommerce_get_template_part('content', 'product');
                    echo!(($i + 1) % 2) ? '</div>' : '';
                    $i++;
                endwhile; // end of the loop.  
                echo (($i) % 2) ? '</div>' : '';
                ?>
                <!-- pagination --> 
                <div class="paginations">
                    <?php do_action('woocommerce_pagination') //echo woo_pagination($args);   ?>
                </div>
            </div>
        <?php elseif (!woocommerce_product_subcategories(array('before' => woocommerce_product_loop_start(false), 'after' => woocommerce_product_loop_end(false)))) : ?>

            <?php woocommerce_get_template('loop/no-products-found.php'); ?>

        <?php endif; ?>



    </div>
</div>
<!-- Content -->

    <?php } // end the the product list when is not the shop page ?>

<div class="hotline-home hotline">
<?php get_sidebar('hotline') ?>                
</div>
<div class="container">

    <div class="row">
        <?php
        wp_reset_query();
        $posts_per_page = 6;
        query_posts(array('posts_per_page' => $posts_per_page));
        if (have_posts())
            while (have_posts()): the_post();
                if (has_post_thumbnail()) {
                    $large_image_url = wp_get_attachment_image_src(get_post_thumbnail_id(), 'large');
                    $url_to_image = $large_image_url[0];
                } else {
                    $url_to_image = bloginfo('template_url') . '/img/no-image-blog-one.png';
                }
                $abs_image = str_replace('http://'.$_SERVER['SERVER_NAME'], $_SERVER['DOCUMENT_ROOT'], $url_to_image);
                if(!file_exists($abs_image)) $url_to_image = str_replace('E12', 'E 12', $url_to_image );
                $abs_image = str_replace('http://'.$_SERVER['SERVER_NAME'], $_SERVER['DOCUMENT_ROOT'], $url_to_image);
                if(!file_exists($abs_image)) $url_to_image = site_url().'/wp-content/uploads/no_image.jpg';
                ?>
                <div class="col-lg-4 col-sm-6 col-12">
                    <div class="news-item">
                        <div class="news-thumb">
                            <a href="<?php the_permalink() ?>">
                                <img src="<?php echo $url_to_image ?>" />
                            </a>
                        </div>
                        <div class="news-title"><a href="<?php the_permalink() ?>"><?php the_title() ?></a></div>
                        <div class="news-description">
                            <?php the_excerpt(''); ?> 
                        </div>
                        <div class="read-more">
                            <a href="<?php the_permalink() ?>">&raquo; Chi tiết</a>
                        </div>

                    </div>
                </div>
            <?php endwhile; ?>

    </div>
</div>
<script type="text/javascript">

    jQuery('.prothumb').click(function() {
        $.blockUI({css: {border: 'none', padding: '15px', backgroundColor: '#000', '-webkit-border-radius': '10px', '-moz-border-radius': '10px', opacity: .5, color: '#fff'}});
        pro_id = jQuery(this).attr('ele');
        jQuery.post('<?php echo site_url() ?>/prodetail', {pro_id: pro_id}, function($data) {
            var $data = $data.replace("/prodetail?", "/shop/?");
            jQuery('#xprodetail').html($data);

            jQuery('form.variations_form').wc_variation_form();
            jQuery('form.variations_form .variations select').change();
            jQuery('.single_add_to_cart_button').html('Đặt hàng');

            jQuery('#prosize .size').on('click', function() {
                var is_same_choose = false;
                for (var i = 0; i < $('.size').length; i++) {
                    if ($(this).attr('itemid') == $($('.size')[i]).attr('itemid') && $($('.size')[i]).hasClass('active'))
                        is_same_choose = true;
                }
                $('.size').removeClass('active');
                if (is_same_choose == false)
                    $(this).addClass('active');
                eleval = jQuery(this).attr('itemid');
                jQuery('#pa_size option').each(function() {
                    console.log(eleval + ' ' + jQuery(this).val());
                    if (jQuery(this).val() === eleval) {
                        jQuery("#pa_size option").removeAttr('selected');
                        jQuery(this).attr('selected', 'selected');
                    }
                });
                jQuery("#pa_size").change();
            });
            jQuery.unblockUI();
        });
    });

    jQuery('#prosize .size').click(function() {
        jQuery('#pa_size').val(jQuery(this).attr('itemid'))
    });
    jQuery('.single_add_to_cart_button').html('Đặt hàng');
    jQuery('.wp-paginate').removeClass('wp-paginate');
    jQuery('.hotline-home').addClass('hotline-inner').removeClass('hotline-home');
</script>
<style type="text/css">
    .ptitle a {
	  color: #000000;
	  line-height: 19px;
	}
    .header {
        padding-bottom: 50px;
    }
   .list-product, .thumb-product {
	  margin-top: -138px;
	}
    .list-product .paginations {
	  margin: 10px 0 20px;
	  width: 300px;
	}
   
   .list-product .paginations ol {
	  float: right;
	  min-width: 200px;
	}
    .blockUI.blockMsg.blockPage h1{
        font-size: 14px;
    }
    .ptitle {
        margin-top: 0px !important;
    }
    .prodetail > div{
        text-align: right;
    }
   .prodetail > div h4 {
	  color: #333333;
	  display: table-cell;
	  font-size: 21px;
	  font-weight: bold;
	  height: 29px;
	  margin: 0;
	  text-align: left;
	  vertical-align: middle;
	}
   
    .header-description > div, .header-description + div{
        overflow: visible;
    }
    
@media (min-width: 768px) and (max-width: 991px) {
    .header-description .title .line_1 {
        font-size: 22px;
        margin-right: 65px;
    }
    .prodetail > div h4{
        padding: 0;
    } 
    .prodetail .btn-custom {
        margin-left: 2px;
    }
    .size {
        font-size: 20px;
        padding: 8px;
        width: 36px;
        height: 36px;
        margin-right: 0px;
    }
    .product-cart {
        background: url(<?php echo site_url() ?>/wp-content/themes/kkfashion/img/ico-cart.png) 65px 5px no-repeat;
    }
    .ptitle {
        margin-top: -7px !important;
        font-size: 12px;
    }
}
@media (max-width: 767px){
    .list-product .paginations .navigation {
        float: none;
        margin-left: 35px;
    }
}
</style>
<?php get_footer('shop'); ?>